var app = getApp()

//常量数据配置
var ConstantData = require('../../common/constant_data.js')
const { toSetMiniAuthSetting } = require('../../utils/commonUtil.js')
Page({
  data: {
    showMaintPlatform: false,
    showUseOtherMini: false,
    platformTipDesc: '',
    balance: 0,
    nickname: '',
    isBuyCard: 0,
    bindCount: 0,
    canIUse: wx.canIUse('open-data'),
    title: "测试",
    isSubOffical: true,
    officalSubImg: ConstantData.CONSTATNT_DATA.OFFICAL_SUB_IMG_URL,
    exitLogin: false
  },

  /**
   * 退出登录
   */
  logout: function (e) {
    let that = this
    wx.showModal({
      title: '温馨提示',
      content: '确定要退出吗?',
      success(res) {
        if (res.confirm) {
          that.logoutHandle()
        } else if (res.cancel) {}
      }
    })
  },

  logoutHandle: function () {
    let that = this
    that.setData({
      exitLogin: true
    })
    app.globalData.exitLoginFlg = true
    app.globalData.pass = null
    app.globalData.nophone = 1
    app.globalData.loginData.nophone = 1
    wx.setStorageSync('exitLogin', true)
    wx.removeStorageSync('pass')
    wx.removeStorageSync('nophone')
    wx.showToast({
      title: '您已退出登录',
      icon: 'none'
    })
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      })
    }, 500)
  },

  callPhone() {
    wx.makePhoneCall({
      phoneNumber: '4006710500',
    })
  },

  /**
   * 跳转到关注公众号的页面
   */
  goFollowMp: function () {
    app.goFollowMp(-200002, '关注公众号-我的')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.userInfo()
    let user_id = wx.getStorageSync('user_id')
    this.setData({
      isSubOffical: app.globalData.isSubOffical,
      user_id: user_id,
      miniVer: ConstantData.CONSTATNT_DATA.MINI_VERSION,
    })
    // 记录展示关注公众号日志
    if (!app.globalData.isSubOffical) {
      app.recordShowMpLog(-200002, '关注公众号-我的')
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  contacted() {
    wx.navigateTo({
      url: '../callCenter/callCenter',
    })
  },
  jumpauthor() {
    var that = this
    wx.navigateTo({
      url: '../../pkgBak/pages/closedSociety/closedSociety',
    })
  },
  onShow: function () {
   let nophone= wx.getStorageSync('nophone')
    if (nophone!=2) {
      this.logoutHandle()
    } else {
      this.userInfo()
    }
  },
  callServer: function () {
    wx.makePhoneCall({
      phoneNumber: '4006710500'
    })
  },
  toHelp: function () {
    wx.navigateTo({
      url: '../../pkgBak/pages/help/help',
    })
  },
  deposit_record: function () {
    wx.navigateTo({
      url: '../user_record/deposit/user_deposit',
    })
  },

  lease_record: function () {
    wx.navigateTo({
      url: '../user_record/lease/user_lease',
    })
  },
  more: function () {
    if (!wx.canIUse('web-view')) {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
      return
    }
    wx.navigateTo({
      url: '../more/about/about',
    })
  },
  depositList: function () {
    wx.navigateTo({
      url: '../deposit_list/deposit_list',
    })
  },
  /**
   * 包时卡记录列表
   */
  timeCardRecord: function () {
    wx.navigateTo({
      url: '../../pkgBak/pages/time_card/record/record',
    })
  },
  /***
   * 代租赁用户记录列表
   */
  agentLeaseRecord: function () {
    wx.navigateTo({
      url: '../../pkgBak/pages/time_card/bind_users/bind_users',
    })
  },
  /**  
   * 获取用户手机号
   */
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg.indexOf('user deny') != -1) {
      that.setData({
        exitLogin: false
      })
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    } else {
      //解密数据并登录成功
    let reqRst = app.executeRequest(function (header) {

        wx.request({
          url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease/auth/wxPhoneAuthLogin',
          header: header,
          data: {
            iv: encodeURIComponent(e.detail.iv),
            encryptedData: encodeURIComponent(e.detail.encryptedData)
          },
          success: function (res) {
            let netErr = app.handleRes(res, that);
            if (netErr) {
              return
            }
            if (res.data.result === 0) {    
              wx.showToast({
                title: '登录成功',
                icon: 'none'
              })
              wx.setStorageSync('nophone', '2');
              app.globalData.loginData.nophone = 2;
              var pt = that.data.pt
              setTimeout(() => {
                if (pt == 'time_card_detail') {
                  //包时卡详情
                  var cardNumber = that.data.card_number
                  wx.navigateTo({
                    url: '../../pkgBak/pages/time_card/detail/time_card_detail?cn=' + cardNumber,
                  })
                  return
                } else if (pt == 'outpatient_add_employer') {
                  //爱心租赁
                  wx.redirectTo({
                    url: '../outpatient_service/manage_employer/manage_employer',
                  })
                } else if (pt == 'outpatient_manage_careworker') {
                  //门诊爱心租赁护工管理
                  wx.redirectTo({
                    url: '../outpatient_service/manage_careworker/manage_careworker',
                  })
                } else if (pt == 'user_center') {
                  //个人中心
                  wx.redirectTo({
                    url: '../user_center/user_center',
                  })
                } else if (pt == 'deposit_record') {
                  //押金记录
                  wx.redirectTo({
                    url: '../user_record/deposit/user_deposit',
                  })
                } else if (pt == 'no_return_blue_timeout') {
                  //押金记录
                  wx.redirectTo({
                    url: '../index/index?pt=no_return_blue_timeout',
                  })
                } else if (pt == 'scan') {
                  wx.redirectTo({
                    url: '../index/index?pt=scan',
                  });
                } else {
                  wx.reLaunch({
                    url: '../index/index',
                  });
                }
              }, 1000)
            } else if (res.data.result === 4) {
              that.setData({
                showdialog: true,
                title: res.data.description,
              })
            } else {
              // that.setData({
              //   showdialog:true,
              //   title:'登录失败'
              // })
              that.setData({
                exitLogin: false
              })
              wx.navigateTo({
                url: '/pages/login/login',
              })
            }
          },
          fail: function (err) {
            that.setData({
              exitLogin: false
            })
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        });

      });

    app.reqNoPassHandle(function() {
        console.log("========>", app.globalData.maintShowFlg)
        if(app.globalData.maintShowFlg) {  
           that.setData({
             platformTipDesc: app.globalData.showMaintDesc,
             showMaintPlatform: app.globalData.showMaintPlatform,
             showUseOtherMini: app.globalData.showUseOtherMini,                      
           })
        } 
     }, reqRst);  

    }

  },
  userInfo: function () {
    var that = this;
    if (app.globalData.loginData && app.globalData.loginData.nophone == 1) {
      //需要重新登录
      that.setData({
        exitLogin: true
      })
      return
    }

    let reqRst = app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_ucenter/userinfo',
        header: header,
        success: function (res) {
          console.log('userInfo--res', res);
          let netErr = app.handleRes(res, that);
          if (netErr) {
             return
          }
          if (res.data.result != 0) {
            that.setData({
              showdialog: true,
              title: res.data.description,
            })
            return
          }
          console.log(788555,res.data.data)
          that.setData({
            exitLogin: false,
            balance: res.data.data.balance,
            nickname: res.data.data.uinfo.userNickName,
            bindCount: res.data.data.bindCount,
            isBuyCard: res.data.data.isBuyCard
          });
        }
      })

    })
    console.log("----userCenter-->", reqRst)
    app.reqNoPassHandle(function() {
       console.log("========>", app.globalData.maintShowFlg)
       if(app.globalData.maintShowFlg) {  
          that.setData({
            platformTipDesc: description,
            showMaintPlatform: app.globalData.showMaintPlatform,
            showUseOtherMini: app.globalData.showUseOtherMini,                      
          })
       }

    }, reqRst);

  },

  closePlatformTip() {
    this.setData({
      showMaintPlatform: false
    })
  },
  closeUseOtherTip() {
    this.setData({
      showUseOtherMini: false
    })
  },

  optCard: function () {
    wx.navigateTo({
      url: 'opt_card/opt_card',
    })
  },

  equUseHelpTip: function () {
    wx.navigateTo({
      url: '../../pkgBak/pages/help_tip/help_tip',
    })
  },

  jumpjump() {
    wx.navigateTo({
      url: '../../pkgBak/pages/interlayer/interlayer?card=51023017',
    })
  },
  //开发票
  invoiceTip: function () {
    wx.navigateTo({
      url: '../../pages/orderInvoice/orderInvoice',
    })
  },
  //赔偿说明
  compensateShow:function(){
    wx.navigateTo({
      url: '/pkgNew/pages/compensateLose/index',
    })
  },
  //意见反馈
  tofeedback:function(){
    wx.navigateTo({
      url: '/pkgNew/pages/opinionFeeback/index',
    })
  }
})
